package com.bt.lms.ui.action.school_education.archives.register;

import com.alibaba.fastjson.JSON;
import com.bt.lms.constants.StuStatusBizTypeConstant;
import com.bt.lms.entity.RegisterInfo;
import com.bt.lms.network.msg.ReqMsg;
import com.bt.lms.network.msg.RespMsg;
import com.bt.lms.ui.vo.school_education.registerInfo.EditRegisterInfoVo;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.control.Alert;
import org.apache.commons.beanutils.BeanUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.Socket;

/**
 * 删除注册信息事件
 */
public class DeleteRegisterVerify implements EventHandler {


    @Override
    public void handle(Event event) {


        byte[] bytes = null;
        //3.组包
        EditRegisterInfoVo editRegisterInfoVo = new EditRegisterInfoVo();

        bytes = handle2Msg(editRegisterInfoVo);

        //组网
        Socket socket = getNet();
        //发送消息
        boolean flag = sengMsg(socket, bytes);

    }

    /**
     * 发送消息
     *
     * @param socket 网
     * @param bytes  要发送的字节数组
     * @return 是否成功
     */
    private boolean sengMsg(Socket socket, byte[] bytes) {
        OutputStream out = null;
        try {
            out = socket.getOutputStream();
            out.write(bytes);
            out.flush();


            //接收响应消息
            InputStream in = socket.getInputStream();
            byte[] buf = new byte[1024];
            in.read(buf);

            RespMsg respMsg = RespMsg.toRespMsg(buf);
            String data = respMsg.getData();
            if (data != null){
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setContentText("删除成功");
                alert.show();
            }


            //关闭资源
            out.close();
            socket.close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return true;
    }

    /**
     * 获取网络
     *
     * @return 网
     */
    private Socket getNet() {
        Socket socket = null;

        try {
            socket = new Socket("127.0.0.1", 8888);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


        return socket;
    }

    /**
     * 组包
     *
     * @param editRegisterInfoVo vo对象
     * @return byte字节数组
     */
    private byte[] handle2Msg(EditRegisterInfoVo editRegisterInfoVo) {
        ReqMsg reqMsg = new ReqMsg();
        reqMsg.setBizType(StuStatusBizTypeConstant.REGISTER_INFO_BIZ_TYPE_DELETE);
        RegisterInfo registerInfo = new RegisterInfo();
        try {
            BeanUtils.copyProperties(registerInfo, editRegisterInfoVo);
            registerInfo.setId(1L);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        String registerStr = JSON.toJSONString(registerInfo);
        reqMsg.setData(registerStr);

        return reqMsg.toBytes();

    }

    /**
     * 验证用户输入
     *
     * @param editRegisterInfoVo vo对象
     * @return 验证后的str
     */
    private String getVerifyInput(EditRegisterInfoVo editRegisterInfoVo) {


        return null;

    }

    /**
     * 获取用户输入
     *
     * @return
     */
    private EditRegisterInfoVo getInput() {


        return null;
    }


}
